<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper indicator-dots="true" autoplay="true" 
			indicator-active-color="rgb(255,255,255)" 
			indicator-color="rgba(255,255,255,0.5)"
			circular="true">
				<swiper-item v-for="item in bannerList" :key="item._id">
					<navigator v-if="item.target == 'miniProgram'" :url="item.url" class="jump" target="miniProgram" :app-id="item.appid">
						<image :src=item.picurl mode="aspectFill"></image>
					</navigator>
					<navigator v-else :url="'/pages/classlist/classlist?' + item.url" class="jump">
						<image :src=item.picurl mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>
		
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical="true" autoplay interval="1500" duration="300" circular>
					<swiper-item v-for="item in wallNewsList" :key=item._id>
						<navigator :url="'/pages/notice/detail?id=' + item._id">
							{{item.title}}
							<view class="right">
								<uni-icons type="right" size="16" color="#333"></uni-icons>
							</view>
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			
		</view>
		
		<view class="select">
			<common-title>
				<template #name>
					<view>每日推荐</view>
				</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="25"></uni-icons>
						<view class="text">
							<uni-dateformat :date=Date.now() format="dd号"></uni-dateformat>
						</view>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x >
					<view class="box" v-for="item in randomList" :key="item._id" @click="goPreviewClick(item._id)">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		
		<view class="theme">
			<common-title>
				<template #name>
					<view>专题精选</view>
				</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onPullDownRefresh, onShareAppMessage, onShareTimeline} from "@dcloudio/uni-app"
import { apiGetBanner, apiGetDayRandom, apiGetWallNews, apiGetClassify } from '@/apis/apis.js';

//跳转到预览页面
const goPreviewClick = (id)=>{
	uni.setStorageSync("storeWallList",randomList.value)
	// console.log(randomList.value);
	uni.navigateTo({
		url:"/pages/preview/preview?id="+id
	})
}

const bannerList = ref([])
const randomList = ref([])
const wallNewsList = ref([])
const classifyList = ref([])

//分享给朋友
onShareAppMessage(()=>{
	return {
		title:"卷毛壁纸手机换肤",
		path:"/pages/index/index",
		imageUrl:"/static/images/logo.png"
	}
})

//分享朋友圈
onShareTimeline(()=>{
	return {
		title:"卷毛壁纸手机换肤",
		imageUrl:"/static/images/logo.png"
	}
})

//下拉刷新
onPullDownRefresh(()=>{
	bannerList.value = []
	wallNewsList.value = []
	randomList.value = []
	classifyList.value = []
	
	getDayRandom();
	getBanner();
	getWallNews();
	getClassify();
	
	uni.stopPullDownRefresh()
})

//获取首页滑动图片
const getBanner = async ()=>{
	let res = await apiGetBanner();
	bannerList.value = res.data
	console.log(bannerList.value);
}

//获取首页每日推荐
const getDayRandom = async ()=>{
	let res = await apiGetDayRandom();
	randomList.value = res.data;
	// console.log(res)
}

//获取首页公告
const getWallNews = async ()=>{
	let res = await apiGetWallNews({select:"true"})
	wallNewsList.value = res.data
	// console.log(res)
}

//获取专题精选
const getClassify = async ()=>{
	let res = await apiGetClassify({select:"true"})
	classifyList.value = res.data
	// console.log(res)
}

getDayRandom();
getBanner();
getWallNews();
getClassify();
</script>

<style lang="scss" scoped>
.homeLayout{
	.banner{
		width: 750rpx;
		padding: 30rpx 0;
		swiper{
			width: 750rpx;
			height: 340rpx;
			swiper-item{
				width: 100%;
				height: 100%;
				padding: 0 30rpx;
				.jump{
					width: 100%;
					height: 100%;
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
	
	.notice{
		width: 690rpx;
		height: 80rpx;
		line-height: 80rpx;
		margin: 0 auto;
		padding: 0 20rpx;
		background-color: #f9f9f9;
		border-radius: 80rpx;
		display: flex;
		.left{
			width: 140rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			:deep(){
				.uni-icons{
					color: $uni-color-brand !important;
				}
			}
			.text{
				padding-left: 10rpx;
				color: $uni-color-brand;
				font-weight: 600;
				font-size: 28rpx;
			}
		}
		.center{
			flex: 1;
			swiper{
				height: 100%;
				swiper-item{
					height: 100%;
					font-size: 30rpx;
					color: #666;
					overflow: hidden;
					white-space:nowrap;
					text-overflow: ellipsis;
				}
			}
			.right{
			width: 70rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			}
		}
		
		
	}
	
	.select{
		padding-top: 50rpx;
		.date{
			display: flex;
			align-items: center;
			justify-content: center;
			:deep(){
				.uni-icons{
					color: $uni-color-brand !important;
				}
			}
			.text{
				margin-left: 10rpx;
				color: $uni-color-brand;
			}
		}
		
		.content{
			width: 720rpx;
			margin-top: 30rpx;
			margin-left: 30rpx;
			scroll-view{
				white-space:nowrap;
				.box{
					width: 200rpx;
					height: 440rpx;
					display: inline-block;
					margin-right: 15rpx;
					image{
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
				.box:last-child{
					margin-right: 30rpx;
				}
			}
		}
	}
	
	.theme{
		padding: 50rpx 0;
		.more{
			font-size: 32rpx;
			color: $uni-color-brand;
		}
		.content{
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			grid-template-columns: repeat(3,1fr);
			gap: 20rpx;
		}
		
	}
}
</style>
